package manager.qrcode;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TimerTask;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeansException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import database.DBService;

import database.pojo.MachineInfo;
import manager.pay.type.PayResultInfo;
import manager.pay.type.ntfWeChatPayInfo;
import manager.rank.InsertToRedis;
import net.socket.server.SocketServer;
import service.IGetQRCode;
import util.MyFactory;
@Component("QRCodeManager")
public class QRCodeManager{
	//@Scheduled(cron = "0 0 0 * * ?")//测试环境需要注释
	public void updateQRCodes() throws Exception{
		List<MachineInfo> l1 = MyFactory.context.getBean("DBService", DBService.class).selectAllMachineInfo();
		IGetQRCode getQRCode = MyFactory.context.getBean("GetQRCode", IGetQRCode.class); 
		for (MachineInfo machineInfo : l1) {
			machineInfo.setQrCodeUrl(getQRCode.getQRCode(machineInfo.getMcahineId()));
			MyFactory.context.getBean("DBService", DBService.class).updateMachineInfo(machineInfo);
		}
	}
	
	//获取微信登录的二维码 
	public String getQRCode(int machineId) throws BeansException, SQLException, URISyntaxException, IOException{
		
		MachineInfo m1 = MyFactory.context.getBean("DBService", DBService.class).selectMachineInfoById(machineId);
		String baseUrl = "http://120.24.67.44/les/release/musicpro/center/req_qrcode/";
		String roomkey ="";
		String DefaultPic = "http://120.24.67.44/les/release/musicpro/center/req_qrcode/default.png";
		
		if(m1.getEndTime()<=System.currentTimeMillis()) {
			System.out.println("xiaoyu  chongxin shengcheng ");
			roomkey = makeRoomkey(machineId);
			String QrcodeUrl =ntfPHPQrcode(roomkey);
			if (QrcodeUrl.equals("")) {
				QrcodeUrl = DefaultPic;
			}
			
			
			//清空歌单redisclean
			MyFactory.context.getBean("InsertToRedis",InsertToRedis.class).cleanSonglistKey(machineId,-1);
			//更新认证码
			MyFactory.context.getBean("DBService",DBService.class).updateRoomKey(machineId,roomkey);
			return QrcodeUrl;
			
		}
		if(m1.getEndTime()>System.currentTimeMillis()) {
			System.out.println("dayu bushengcheng zhijie jin ");
			return baseUrl+m1.getRoomKey()+".png";
		}
		
		return DefaultPic;
	}

	public String makeRoomkey(int machineId) {
		String Salt = "2018";
		return String.format("%d%s%d",System.currentTimeMillis(),Salt,machineId);
	}
	
	public String ntfPHPQrcode(String roomKey) throws BeansException, SQLException, URISyntaxException, IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();   
        URI uri = new URIBuilder("http://120.24.67.44/les/release/musicpro/center/get_qrcode.php?")//测试环境
        		.addParameter("roomKey",roomKey)
        		.build();
        HttpGet httpGet = new HttpGet(uri);
        String result = "";
        //response 对象
        CloseableHttpResponse response = null;
        try {
            // 执行http get请求
            try {
				response = httpclient.execute(httpGet);
				 // 通过返回对象获取返回数据
	            HttpEntity entity = response.getEntity();
	            // 通过EntityUtils中的toString方法将结果转换为字符串
	            result = EntityUtils.toString(entity);
	            
	            return result;
			} catch (IOException e) {
				e.printStackTrace();
			}
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
		return result;
		
	}
}
